const fs = require('fs');
const path = require('path');

module.exports = function (pagePath, name) {
  const reducerPath = pagePath + '/reducer';
  if (!fs.existsSync(reducerPath)) {
    fs.mkdirSync(reducerPath);
  }
  const reducerName = `${name}Reducer`;

  const reducerFile = reducerPath + `/${reducerName}.js`;
  if (!fs.existsSync(reducerFile)) {
    const template = [];
    template.push(`import { fromJS } from 'immutable';`);
    template.push(`import { Utils } from '../../../lib';`);
    template.push(`import { ${name}ActionType } from '../const';`);
    template.push(``);
    template.push(`const { createReducer } = Utils;`);
    template.push(``);
    template.push(`const initialState = fromJS({`);
    template.push(``);
    template.push(`});`);
    template.push(``);
    template.push(`const handlers = {`);
    template.push(``);
    template.push(`};`);
    template.push(``);
    template.push(`export default createReducer(initialState, handlers);`);
    template.push(``);

    fs.writeFileSync(reducerFile, template.join('\n'));
  }

  const indexPath = reducerPath + '/index.js';
  if (!fs.existsSync(indexPath)) {
    const template = [];
    template.push(`import ${reducerName} from './${reducerName}';`);
    template.push(`export default ${reducerName} ;`);
    template.push(``);
    // template.push(`import { fromJS } from 'immutable';`);
    // template.push(`import ${reducerName} from './${reducerName}';`);
    // template.push(``);
    // template.push(`const initialState = fromJS({ `);
    // template.push(` ${reducerName}: undefined`);
    // template.push(`});`);
    // template.push(``);
    // template.push(`export default (state = initialState, action) =>`);
    // template.push(`  fromJS({`);
    // template.push(`    ${reducerName}: ${reducerName}(state.get('${reducerName}'), action)`);
    // template.push(`  });`);

    fs.writeFileSync(indexPath, template.join('\n'));
  }
}
